package net.zjitc.ui;

import net.zjitc.domain.Student;
import net.zjitc.service.StudentService;
import net.zjitc.service.impl.StudentServieImpl;
import net.zjitc.utils.DateUtils;

import java.text.ParseException;
import java.util.List;
import java.util.Scanner;

public class StudentUI {
    public static void main(String[] args) {
        //测试查找所有
        StudentService studentServie = new StudentServieImpl();
        //输入
        Scanner in = new Scanner(System.in);
        while (true){
            showUI();
            int choose = in.nextInt();
            switch (choose){
                case 1:
                    try {
                        List<Student> studentList = studentServie.findAll();
                        System.out.println("完成查询功能");
                        for (Student s : studentList) {
                            System.out.println(s);
                        }
                    }catch (Exception e){
                        System.out.println("查询失败！！");
                    }

                    break;
                case 2:
                    System.out.println("按name,age,gander,address,birthday输入");
                    //按name,age,gander,address,birthday
                    String name = in.next();
                    int age = in.nextInt();
                    String gander = in.next();
                    String address = in.next();
                    String birthday = in.next(); //日期按字符串的格式输入
                    //转换 Math.abs():日期与字符串的转换封装成一个工具类
                    Student student = null;
                    try {
                        student = new Student(name,age,gander,address, DateUtils.stringToDate(birthday));
                    }catch (Exception e){
                        System.out.println("格式不对！");
                        break; //格式不对就不添加
                    }

                    boolean result  = studentServie.add(student);
                    if (result){
                        System.out.println("添加成功！");
                    }else {
                        System.out.println("添加失败！！");
                    }
                    break;
                case 3:

                    //修改的话，一定要先查询的？根据名字查询，按名字修改
                    System.out.println("请输入要修改的名字 ");
                    name = in.next();
                    //先查询，输出原有信息，先查询
                    Student s = studentServie.findByName(name);
                    if (s==null){
                        System.out.println("查无此人！");
                        break;
                    }
                    System.out.println(s); //先看一下信息
                    //（业务需求）只能修改以下信息
                    System.out.println("输入修改的信息：age,gander,address,birthday");
                    age = in.nextInt();
                    gander = in.next();
                    address = in.next();
                    birthday = in.next();

                    //为当前学生设置新的信息
                    s.setAge(age);
                    s.setAddress(address);
                    s.setGander(gander);
                    try {
                        s.setBirthday(DateUtils.stringToDate(birthday));
                        //更新学生
                        boolean update = studentServie.update(s);
                        if (update){
                            System.out.println("修改成功");
                        }else {
                            System.out.println("修改失败");
                        }

                    } catch (ParseException e) {
                        System.out.println("格式错误！");
                        break;
                    }
                    break;
                case 4:
                    System.out.println("完成删除功能");
                    System.out.println("请输入删除的学生的姓名：");

                    break;
                case 5:
                    System.out.println("完成退出功能！");
                    return;
                default:
                    System.out.println("提示选择错误，是否继续");
            }
        }

    }

    //界面
    private static void showUI() {
        System.out.println("------欢迎进入学生管理系统------");
        System.out.println("请选择功能：");
        System.out.println("1:查询所有");
        System.out.println("2:添加学生");
        System.out.println("3:修改学生");
        System.out.println("4:删除学生");
        System.out.println("5:退出系统");

    }

}
